package com.smh.demo1.a03;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @author shiminghui
 * @date 2025/4/10 20:01
 * @description: TODO
 */
public class TestMethodTemplate {

    public static void main(String[] args) {

        MyBeanFactory myBeanFactory = new MyBeanFactory();
        myBeanFactory.addProcessor(new BeanPostProcessor() {
            @Override
            public void inject(Object bean) {
                System.out.println("依赖注入");
            }
        });

        Object bean = myBeanFactory.getBean();
        System.out.println(bean);

    }


    static interface BeanPostProcessor {
        public void inject(Object bean);
    }

    static class MyBeanFactory {
        private final static Logger log = LoggerFactory.getLogger(MyBeanFactory.class);
        private final List<BeanPostProcessor> processors = new ArrayList<>();

        public Object getBean() {
            Object bean = new Object();
            log.info("创建bean实例");
            log.info("注入bean属性");
            // 模板方法
            for (BeanPostProcessor processor : processors) {
                processor.inject(bean);
            }
            log.info("初始化bean");
            return bean;
        }

        public void addProcessor(BeanPostProcessor processor) {
            processors.add(processor);
        }


    }

}
